<template>
	<view>
		<view style="background-color: white;padding: 20rpx 30rpx;">
			<u-form :model="form" ref="uForm" label-width="auto">
				<u-form-item label="订单编号" prop="orders_num">
					<view style="display: flex;justify-content: flex-end;">
						<u-input v-model="form.orders_num" placeholder="选填，默认自动生成"   input-align="right"/>
					</view>
				</u-form-item>
				<u-form-item label="订单来源" prop="orders_source_id">
					<view style="display: flex;justify-content: flex-end;" @click="show.orders_source_select=true">
						<u-picker v-model="show.orders_source_select" mode="selector" :range="orders_source_list" range-key="label"
							@confirm="(e)=>{
								confirmSource(e);
								value1=orders_source_list[e[0]].label;
								form.orders_source_id=orders_source_list[e[0]].value;
							}" :params="{year: true,
						month: true,
						day: true,
						hour: true,
						minute: true}"></u-picker>
						<u-input v-model="value1" :disabled="true" placeholder="点击此处选择订单来源" type="select"
							@click="show.orders_source_select=true"  input-align="right"/>
					</view>
				</u-form-item>
				<u-form-item label="取车时间" prop="take_date">
					<view style="display: flex;justify-content: space-between;" @click="show.take_date_select=true">
						<u-picker v-model="show.take_date_select" mode="time" @confirm="(e)=>{
							form.take_date=e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute;
							form.takeReturn1.takereturen_date=e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute;
							if(form.take_date&&form.return_date){
								form.use_day=strDateToTime(form.take_date,form.return_date)
							}
						}" :params="{year: true,
					month: true,
					day: true,
					hour: true,
					minute: true}"></u-picker>
						<u-input v-model="form.take_date" placeholder="点击此处选择取车时间" :disabled="true" type="select"
							@click="show.take_date_select=true" />
					</view>
				</u-form-item>
				<u-form-item label="还车时间" prop="return_date">
					<view style="display: flex;justify-content: space-between;" @click="show.return_date_select=true">
						<u-picker v-model="show.return_date_select" mode="time" @confirm="(e)=>{
							form.return_date=e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute;
							form.takeReturn2.takereturen_date=e.year+'-'+e.month+'-'+e.day+' '+e.hour+':'+e.minute;
							if(form.take_date&&form.return_date){
								form.use_day=strDateToTime(form.take_date,form.return_date)
							}
						}" :params="{year: true,
					month: true,
					day: true,
					hour: true,
					minute: true}"></u-picker>
						<u-input v-model="form.return_date" placeholder="点击此处选择还车时间" :disabled="true" type="select"
							@click="show.return_date_select=true" />
					</view>
				</u-form-item>
				<u-form-item label="用车天数" prop="use_day">
					<view style="display: flex;justify-content: flex-end;">
						<u-input v-model="form.use_day"  :disabled="true" input-align="right" placeholder="无需输入，自动计算" />
					</view>
				</u-form-item>
				<u-form-item label="异地取还车" prop="nonlocal_take_return">
					<view style="display: flex;justify-content: flex-end;">
						<u-radio-group v-model="form.nonlocal_take_return">
							<view style="display: flex;">
								<u-radio
									v-for="(item, index) in [{value:1,name:'本地取还车'},{value:2,name:'异地取还车'}]"
									:key="index" :name="item.value">
									{{item.name}}
								</u-radio>
							</view>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item label="取车地点" :border-bottom="false">
					<view style="display: flex;justify-content: flex-start;" @click="$refs.selectAddress.show('takeReturn1')">
						<view>
							<u-input v-model="form.takeReturn1.location_name" placeholder="点击此处选择"  :disabled="true" input-align="left" @click="$refs.selectAddress.show('takeReturn1')" />
						</view>
					</view>
				</u-form-item>
				<view v-if="form.takeReturn1.location_address" style="color: #909399;">
					{{form.takeReturn1.location_address}}
				</view>
				<u-form-item label="取车方式" prop="take_type">
					<view style="display: flex;justify-content: flex-end;">
						<u-radio-group v-model="form.take_type">
							<view style="display: flex;">
								<u-radio
									v-for="(item, index) in [{value:1,name:'上门送车'},{value:2,name:'客户自取'}]"
									:key="index" :name="item.value">
									{{item.name}}
								</u-radio>
							</view>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item label="还车地点" :border-bottom="false">
					<view style="display: flex;justify-content: flex-start;" @click="$refs.selectAddress.show('takeReturn2')">
						<view>
							<u-input v-model="form.takeReturn2.location_name" placeholder="点击此处选择"  :disabled="true" input-align="left" @click="$refs.selectAddress.show('takeReturn2')"/>
						</view>
					</view>
				</u-form-item>
				<view v-if="form.takeReturn2.location_address" style="color: #909399;">
					{{form.takeReturn2.location_address}}
				</view>
				<u-form-item label="还车方式" prop="return_type">
					<view style="display: flex;justify-content: flex-end;">
						<u-radio-group v-model="form.return_type">
							<view style="display: flex;">
								<u-radio
									v-for="(item, index) in [{value:1,name:'上门收车'},{value:2,name:'客户送车'}]"
									:key="index" :name="item.value">
									{{item.name}}
								</u-radio>
							</view>
						</u-radio-group>
					</view>
				</u-form-item>
				<u-form-item >
					<u-button @click="submit" type="primary">保存返回</u-button>
				</u-form-item>
			</u-form>
			
		</view>
		<selectAddress ref="selectAddress" @select="(e)=>{
			form[e.type].location_name=e.title;
			form[e.type].location_address=e.address;
			form[e.type].location_long=e.location.lng;
			form[e.type].location_lat=e.location.lat;
			form[e.type].adcode=e.adcode;
			onSelectAddress(e)
		}"></selectAddress>
		
		<u-toast ref="uToast"></u-toast>
		<myModal ref="myModal"></myModal>
	</view>
</template>

<script>
	import dateUtils from '../../utils/dateUtils';
	export default {
		data() {
			return {
				value1: '',
				value2: '',
				orders_id:'',
				show: {
					orders_source_select: false,
					take_date_select: false,
					return_date_select: false
				},
				orders_source_list: [{
						id: 1,
						label: '后台录入',
						value: 1
					},
					{
						id: 2,
						label: '系统商城',
						value: 2
					},
					{
						id: 3,
						label: '哈罗',
						value: 3
					},
					{
						id: 4,
						label: '飞猪',
						value: 4
					},
					{
						id: 5,
						label: '携程',
						value: 5
					},
					{
						id: 6,
						label: '滴滴打车',
						value: 6
					},
					{
						id: 7,
						label: '第三方推荐',
						value: 7
					}
				],
				form: {
					orders_num: '',
					orders_source_id: 1,
					take_date: '',
					return_date: '',
					use_day: '',
					nonlocal_take_return:1,
					take_type:1,
					return_type:1,
					takeReturn1:{
						location_name:'',
						location_address:'',
						location_long:'',
						location_lat:'',
						adcode:'',
						takereturen_date:'',
						take_returen_type:1,
						action_type:''
					},
					takeReturn2:{
						location_name:'',
						location_address:'',
						location_long:'',
						location_lat:'',
						adcode:'',
						takereturen_date:'',
						take_returen_type:2,
						action_type:''
					}

				},
				rules: {
					item_name: [{
						required: true,
						message: '请输入名称',
						trigger: ['blur', 'change']
					}],
				}
			}
		},
		mounted() {
			this.value1=this.orders_source_list.find(e=>e.id==1).label;
		},
		onLoad(options) {
			if (options.orders_id) {
				this.orders_id = options.orders_id;
			}
		},
		methods: {
			onSelectAddress(e){
				console.log(this.form.takeReturn1);
				console.log(this.form.takeReturn2);
			},
			submit(){
				var that = this;
				this.form.orders_id=this.orders_id;
				that.$http.post('/orders/tempOrdersAddLeaseForm',this.form, (res) => {
					if (res.data.success) {
						that.$refs.uToast.show({
							title: '添加成功',
							type: "success"
						});
						uni.$emit("getOrdersInfo", that.data);
						uni.navigateBack();
					} else {
						that.refs.myModal.show('请求失败', res.data.result.message);
					}
				});
			},
			
			confirmSource(e){
				console.log(e);
			},
			strDateToTime2(take_date, return_date) {
				return dateUtils.strDateToTime2(take_date, return_date);
			},
			strDateToTime(take_date, return_date) {
				return dateUtils.strDateToTime(take_date, return_date);
			},
		},
		onReady() {
			// console.log("ready");
			// console.log(this.$refs.uForm);
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>

</style>